<?php

namespace app\api\controller\v50;

use app\api\controller\v13\Auth2;
use app\common\cachemodel\Goods;
use app\common\cachemodel\name_analyze\Zl9NameAnalyzeRecodeBack;
use app\common\cachemodel\name_analyze\Zl9NameAnalyzeRecord;
use app\common\cachemodel\ZhunleActivity;
use app\common\lib\exception\ApiException;
use app\common\lib\Util;
use think\Env;
use think\response\Json;

class NameAnalyze extends Auth2
{
    /**
     * 获取 最近名字评分信息
     * @return \think\response\Json
     * @throws \Exception
     */
    public function getAnalyzeInfoLately()
    {
        $zl9NameAnalyzeRecordModel = new Zl9NameAnalyzeRecord();
        if ($this->user)
        {
            $pageData = $zl9NameAnalyzeRecordModel->pageDataIds($this->user['id'], 1, 1);
            $id = current($pageData['data']);
        } else
        {
            $id = 0;
        }
        if ($id)
        {
            $data = $zl9NameAnalyzeRecordModel->getInfoById($id);
            if (in_array($data['status'], [1, 3], true) && $this->request->time() - $data['start_time'] > 300)
            {   // 计算中 或 计算失败
                $zl9NameAnalyzeRecordModel->UpdateData($id, ['status' => 1, 'start_time' => $this->request->time(), 'update_time' => $this->request->time()]);
                Util::queuePush('horoscope_name_analyze', ['id' => $id]);
            }
            $recodeData = (new Zl9NameAnalyzeRecodeBack())->getInfoById($data['back_rid']);
            $resData = [
                'id' => $id,
                'rid' => $data['rid'],
                'name' => $recodeData['recode_info']['name'],
                'year' => (int)$recodeData['recode_info']['year'],
                'month' => (int)$recodeData['recode_info']['month'],
                'day' => (int)$recodeData['recode_info']['day'],
                'hour' => (int)$recodeData['recode_info']['hour'],
                'minute' => (int)$recodeData['recode_info']['minute'],
                'name1' => $data['surname'],
                'name2' => $data['name'],
            ];
        } else
        {
            $resData = [
                'id' => 0,
                'rid' => 0,
                'name' => '',
                'year' => 0,
                'month' => 0,
                'day' => 0,
                'hour' => 0,
                'minute' => 0,
                'name1' => '',
                'name2' => '',
            ];
        }
        return show(config('code.success'), '请求成功', $resData);
    }

    /**
     * 获取取名字信息
     * @return Json
     * @throws ApiException
     */
    public function getCreateNameIntro()
    {
        $host = Env::get('host.domain');
        $GoodsCacheModel = new Goods();
        $goods = $GoodsCacheModel->batchGetInfoByIds([165, 166]);
        (new ZhunleActivity())->goodsActiveInfo(35, $goods);
        $goodsInfo = $GoodsCacheModel->goodsInfoArr($this, 35, $goods, [], [], ['isGoodInfo' => true, 'start_time' => '', 'rid' => 0, 'vip_level' => 0]);
        $resData = [
            'personal_demo_url' => $host . '/web/horoscope_demo/createnamepersonal',
            'company_demo_url' => $host . '/web/horoscope_demo/createnamecompany',
            'personal_btn_des' => ($goodsInfo['data'][165]['price'] * 10) . '币分析（至少4个名字推荐）',
            'company_btn_des' => ($goodsInfo['data'][166]['price'] * 10) . '币分析（至少4个名字推荐）',
            'personal_gid' => 165,
            'company_gid' => 166,
        ];
        return show(config('code.success'), '请求成功', $resData);
    }
}
